package com.yxdj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxdj.service.api.domain.MainCategory;
import com.yxdj.service.api.domain.ServiceItem;
import com.yxdj.service.api.domain.SubCategory;
import com.yxdj.service.mapper.MainCategoryMapper;
import com.yxdj.service.mapper.ServiceItemMapper;
import com.yxdj.service.mapper.SubCategoryMapper;
import com.yxdj.service.service.SubCategoryService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 韩总
* @description 针对表【sub_category(子分类表)】的数据库操作Service实现
* @createDate 2025-03-20 14:44:40
*/
@Service
public class SubCategoryServiceImpl extends ServiceImpl<SubCategoryMapper, SubCategory>
    implements SubCategoryService{

    @Autowired
    private MainCategoryMapper mainCategoryMapper;

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    @Override
    public List<SubCategory> getPageList(SubCategory subCategory) {
        LambdaQueryWrapper<SubCategory> subCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        subCategoryLambdaQueryWrapper.like(subCategory.getSubCategoryName() != null, SubCategory::getSubCategoryName, subCategory.getSubCategoryName());
        List<SubCategory> subCategories = baseMapper.selectList(subCategoryLambdaQueryWrapper);
        if (subCategories != null) {
            for (SubCategory subCategory1 : subCategories) {
                MainCategory mainCategory = mainCategoryMapper.selectById(subCategory1.getMainCategoryId());
                subCategory1.setMainCategoryName(mainCategory.getMainCategoryName());
            }
        }
        return subCategories;
    }

    @Override
    public List<SubCategory> getSubCategoryList(Integer limit) {
        List<SubCategory> subCategoryList = baseMapper.getSubCategoryList(limit);
        if(!Collections.isEmpty(subCategoryList)){
            for (SubCategory subCategory : subCategoryList) {
                //查询当前子分类下的前2个服务项目
               List<ServiceItem> serviceItemList = serviceItemMapper.getServiceItemList(subCategory.getId());
               if(!Collections.isEmpty(serviceItemList)){
                   subCategory.setServiceItemList(serviceItemList);
               }
            }
        }
        return subCategoryList;
    }
}




